package com.ysd.lis.controller.bac;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.bac.BacGermMorphology;
import com.ysd.lis.entity.bac.BacMorphologyType;
import com.ysd.lis.request.bac.MorphologyTypeParam;
import com.ysd.lis.service.bac.BacGermMorphologyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菌落形态 前端控制器
 * </p>
 *
 * @author wanghao
 * @since 2024-04-07
 */
@RestController
@RequestMapping("/bac/germ/morphology")
public class BacGermMorphologyController extends BaseController {

    @Autowired
    private BacGermMorphologyService bacGermMorphologyService;


    @ApiOperation("获取菌落形态（分页）")
    @GetMapping("/findPageList")
    public Result findPageList(MorphologyTypeParam morphologyTypeParam) {
        return bacGermMorphologyService.findPageList(morphologyTypeParam);
    }

    @ApiOperation("通过形态id获取菌落形态")
    @GetMapping("/findListByMorphologyId")
    public Result findListByMorphologyId(String id) {
        return bacGermMorphologyService.findListByMorphologyId(id);
    }


    @ApiOperation("修改或新增菌落形态")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody BacGermMorphology morphologies) {
        bacGermMorphologyService.saveOrUpdate(morphologies);
        return Result.succ(1, "保存成功", morphologies);
    }

    @ApiOperation("批量修改或新增菌落形态")
    @PostMapping("/saveOrUpdateBatch")
    public Result saveOrUpdateBatch(@RequestBody List<BacGermMorphology> morphologies) {
        boolean b = bacGermMorphologyService.saveOrUpdateBatch(morphologies);
        if (b) {
            return Result.succ(1, "保存成功", "");
        } else {
            return Result.succ(400, "保存失败", "");
        }
    }


    @ApiOperation("批量删除菌落形态")
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<BacGermMorphology> morphologyType) {
        List<String> ids = morphologyType.stream().filter(item -> StringUtils.isNotBlank(item.getId())).map(BaseEntity::getId).collect(Collectors.toList());
        boolean b = bacGermMorphologyService.removeByIds(ids);
        if (b) {
            return Result.succ(1, "保存成功", "");
        } else {
            return Result.succ(400, "保存失败", "");
        }
    }


    @ApiOperation("保存形态")
    @PostMapping("/saveMorphologys")
    public Result saveMorphologys(@RequestBody BacMorphologyType bacMorphologyType) {
        return bacGermMorphologyService.saveMorphologys(bacMorphologyType);
    }

}
